package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.MmrbT;
import cn.e4j.modular.crm.service.MmrbTService;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 设备BOM维护管理
 * Created by shixc on 2019-1-17
 */
@Controller
@RequestMapping("/crm/gwai620")
public class Gwai620Controller extends BaseController {

    @Autowired
    private MmrbTService mmrbTService;

    /**
     * 跳转设备BOM管理页面
     */
    @RequiresPermissions("crm:gwai620:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai620.html";
    }

    /**
     * 查询设备BOM信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwai620:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("MMRBENT", shiroUser.getDBC());
        List<Map<String, Object>> result = mmrbTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwai620:init")
    @ResponseBody
    public ResponseData add(MmrbT mmrbT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        // 判断编号是否重复
        MmrbT mmrb = mmrbTService.getByKey(shiroUser.getDBC(), mmrbT.getMMRB001(), mmrbT.getMMRB002(), mmrbT.getMMRB003(),
                mmrbT.getMMRB004(), mmrbT.getMMRB005(), mmrbT.getMMRB016(), mmrbT.getMMRB017(), mmrbT.getMMRB024());
        if (mmrb != null) {
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode(),
                    CrmExceptionEnum.EXISTED_THE_CODE.getMessage());
        }
        mmrbTService.add(mmrbT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwai620:init")
    @ResponseBody
    public ResponseData edit(MmrbT mmrbT) {
        mmrbTService.edit(mmrbT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai620:init")
    @ResponseBody
    public ResponseData del(MmrbT mmrbT) {
        mmrbTService.delete(mmrbT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai620:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<MmrbT> mmrbTList = inDto.getReqList(MmrbT.class);
        mmrbTService.delDataBatch(mmrbTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取产品资料列表
     */
    @ResponseBody
    @RequestMapping("/getGwamList")
    public Dto getGwamList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAMENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = mmrbTService.getGwamList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取客户信息列表
     */
    @ResponseBody
    @RequestMapping("/getGwakList")
    public Dto getGwakList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAKENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = mmrbTService.getGwakList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取产品信息列表
     */
    @ResponseBody
    @RequestMapping("/getMmraList")
    public Dto getMmraList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("MMRAENT", shiroUser.getDBC());
        List<Map<String, Object>> list = mmrbTService.getMmraList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 查询设备BOM信息列表
     */
    @RequestMapping("/getMmra")
    @ResponseBody
    public ResponseData getMmra(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ent", shiroUser.getDBC());
        List<Map<String, Object>> result = mmrbTService.getMmraList(page, inDto);
        return new PageReData(page, result);
    }
}
